/* 游戏单位表现基类 */
var FightActor = cc.Node.extend({

    kGraphType: { 				//动画类型
        sp: 1,
        armature: 2,
        frame: 3
    },

    DEBUG: undefined,

    DEFAULT_RES: {				//为加载实际表型动画资源时的默认表现
        sp: UtilRes.misc("dsp"),
        armature: "darmat",
        frame: "dframe"
    },

    ctor: function () {
        cc.Node.prototype.ctor.call(this)
        
        this._action = ""
        this._default_actname = "",
            this._aniloopcb = {}
        this._anicompcb = {}

        this._grapsize = null		//graph size
        this._graphtype = 0			//graph type
        this._graphfiles = 0		//graph files
        this._graph = null   		//graph node
    },

    /**
     * 执行对应的动作
     * @param {string}actname 动作名称
     */
    play: function (actname) {
        if (this._graphtype == this.kGraphType["armature"]) {
            if (this._graph instanceof ccs.Armature) {
                if (actname) { this._graph.getAnimation().play(actname) }
                else { this._graph.getAnimation().playWithIndex(0) }
            }
        }
        else if (this._graphtype == this.kGraphType["frame"]){
            if (this._graph instanceof cc.Sprite) {
                if (actname) {
                    var cacheani = cc.animationCache.getAnimation(actname);
                    var retani;
                    if (cacheani){
                        retani = cacheani;
                    }
                    else{
                        retani = d.aniFrame(actanme);
                    }
                    this._graph.runAction(retani);
                }
            }
        }
    },

    /**
     * 设置动作列表
     * @param {list}actlist 动作名称列表
     */
    setActList: function (actlist) {
        //init action-func  on%Action%
        var self = this
        var act = ""
        for (var i = actlist.length - 1; i >= 0; i--) {
            act = actlist[i]
            var onfunc = "on" + act
            var dofunc = "_do" + act
            self[onfunc] = (function () {
                return function () { } 		//support for outside
            })
            self[dofunc] = (function () {
                var tmp = act
                return function () {
                    self._action = tmp
                    if (self._graph instanceof ccs.Armature) {
                        if (self._graph.getAnimation().getCurrentMovementID() != tmp) {
                            self.play(tmp)
                        }
                    }
                    self["on" + tmp]()
                    return self
                }
            })()

        }
    },

    /**
     * 设置默认动作（默认动作会在动画资源加载完成后没有设定动作的时候默认播放）
     * @param {string}actname 动作名称
     */
    setDefaultAct: function (actname) {
        this._default_actname = actname
    },

    /**
     * 设置armature的表现
     * @param {list}graphfile 动画相关的文件
     */
    setArmatureGraph: function (graphfile) {
        this._graphfiles = Array.prototype.slice.apply(arguments)[0]
        this._graphtype = this.kGraphType["armature"]
        this._graph = this._graphPrecrt(this._graphfiles)
        return this
    },

    /**
     * 设置frame的表现
     */
    setFrameGraph: function (framefile) {
        this._graphfiles = Array.prototype.slice.apply(arguments)[0]
        this._graphtype = this.kGraphType["frame"]
        this._graph = this._graphPrecrt(this._graphfiles)
        return this;
    },

    /**
     * 设置动画回调
     * @param {string}actionname 动作名称
     * @param {cb}function 动作回调
     */
    setAniLoopcb: function (actionname, cb) {
        this._aniloopcb[actionname] = cb
    },

    //设置动画完成回调
    setAniCompcb: function (actionname, cb) {
        this._anicompcb[actionname] = cb
    },

    //获得当前动作
    getAction: function () {
        return this._action
    },

    //获得表现节点大小
    getSize: function () {
        return this._grapsize
    },

    //获得实际表现节点
    getGraph: function () {
        return this._graph
    },

    //调试layer
    debugLayer: function () {
        if (this.DEBUG == false) return
        if (this._debuglayer) {
            this._debuglayer.setContentSize(this._grapsize)
        }
        else {
            var size = this._grapsize
            this._debuglayer = d.colorlayer(cc.color(100 * cc.random0To1(), 100 * cc.random0To1(), 100 * cc.random0To1(), 100), size.width, size.height)
            this._debuglayer.addTo(this).pos(-size.width / 2, 0)
        }
    },

    //注册动画回调
    registerArmCb: function () {
        var self = this
        if ((self._graphtype == self.kGraphType["armature"]) && (self._graph instanceof ccs.Armature)) {
            self._graph.getAnimation().setMovementEventCallFunc(c_func(this._armatureCallback.bind(this)))

        }
    },

    //角色动画回调
    _armatureCallback: function (armature, movementType, movementID) {
        if (movementType == ccs.MovementEventType.loopComplete) {
            if (this._aniloopcb[movementID]) {
                this._aniloopcb[movementID]()
            }
        }
        else if (movementType == ccs.MovementEventType.complete) {
            if (this._anicompcb[movementID]) {
                this._anicompcb[movementID]()
            }
        }
    },

    //更新动画文件
    _updateAni: function () {
        var daction = this._default_actname || ""
        if (this._action != "") daction = this._action
        if (daction == "") {
            return
        }
        this.play(daction)
    },

    //使用临时文件临时创建
    _graphPrecrt: function (graphfile) {
        //load resource and show 
        var ret
        if (this._graphtype == this.kGraphType["armature"]) {
            ret = this._loadArmatures(Array.prototype.slice.apply(arguments)[0])
        }
        else if (this._graphtype == this.kGraphType["frame"]) {
            ret = this._loadFrame(Array.prototype.slice.apply(arguments[0]))
        }
        if (!ret) {
            ret = d.sp(this.DEFAULT_RES["sp"]).addTo(this)
            this._grapsize = ret.size()
        }
        return ret
    },

    //加载frame动画文件
    _loadFrame: function(framefiles){
        var self = this;
        var frameres = Array.prototype.slice.apply(arguments)[0]
        var framename = cc.path.basename(armares[0]).split("_")[0]

        dw.logger.debug("framename is --->>%s", framename);
        

    },

    //加载armature动画文件
    _loadArmatures: function (grafiles) {
        var self = this
        var armares = Array.prototype.slice.apply(arguments)[0]
        var armaname = cc.path.mainFileName(cc.path.basename(armares[0]))
        var eventid = armaname + "_isloading"
        var jfile = ""
        var ismeres = true
        var islcres = true
        var anillistneer = null
        var tres
        var btype
        for (var i = armares.length - 1; i >= 0; i--) {
            tres = armares[i]
            btype = cc.path.extname(tres)

            if (btype == ".ExportJson") {
                jfile = tres
            }

            if (btype == ".ExportJson" || btype == ".png") {
                if (!cc.loader.getRes(tres) && btype == ".png") {
                    islcres = false
                    ismeres = false
                }
                else {
                    if (btype == ".ExportJson") {
                        if (!ccs.armatureDataManager.getArmatureData(armaname)) {
                            ismeres = false
                        }
                    }
                    else if (btype == ".png") {
                        if (!cc.textureCache.getTextureForKey(tres)) {
                            ismeres = false
                        }
                    }


                }
            }
        }

        var rshow = function () {
            if (self._graph) {
                self._graph.clear()
                self._graph = null
            }
            d.dispatch(eventid)
            if (anillistneer != null) {
                cc.eventManager.removeListener(anillistneer)
                anillistneer = null
            }
            self._graph = d.aniBone(armaname).addTo(self, -2)
            self._grapsize = self._graph.size()

            self.debugLayer()
            self._updateAni()
            self.registerArmCb()
            self._graph.setScale(0.7, 0.7)


            var tarbone = self._graph.getBone()

            return self._graph
        }
        var donoting = function () { }
        var memload = function () {
            ccs.armatureDataManager.addArmatureFileInfoAsync(jfile, rshow, self)
        }
        if ((ismeres && ismeres) == true) {
            return rshow()
        }
        else {
            if (islcres == false) {
                cc.loader.load(armares, donoting, memload)
            }
            else {
                cc.warn("Other func doing ani load!Wait 1 frame")
                anillistneer = d.addListener(eventid, self, function () {
                    self.delayCall(rshow)
                })
            }
        }
    },

    //复写setPosition 对FightActor的localZ左边按Y值排序
    setPosition: function (x, y) {
        this._super.apply(this, arguments)
        this.setLocalZOrder(2000 - y)
    }
})
